package com.hex.liyue.giftmodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.giftmodule.adapter.AdapterGiveAwayGifts;
import com.hex.liyue.giftmodule.bean.MyGiftsResultBean;
import com.hex.liyue.giftmodule.presenter.GiveAwayGiftsPresenter;
import com.hex.liyue.giftmodule.presenter.IGiveAwayGiftsView;
import com.hex.liyue.lookingdatemodule.activity.InSomeDetailsActivity;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by WJ on 2016/6/18.
 * 赠送礼物的界面
 */
public class GiveAwayGiftsActivity extends BaseActivity implements IGiveAwayGiftsView, AdapterGiveAwayGifts.OnItemClickListener {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.rv_my_gifts)
    RecyclerView rvMyGifts;
    @Bind(R.id.tv_give_away)
    TextView tvGiveAway;
    @Bind(R.id.iv_all)
    ImageView ivAll;
    @Bind(R.id.tv_all)
    TextView tvAll;
    @Bind(R.id.tv_total_price)
    TextView tvTotalPrice;
    @Bind(R.id.ll_store)
    LinearLayout llStore;

    private GiveAwayGiftsPresenter giveAwayGiftsPresenter;
    private MyGiftsResultBean myGiftsResultBean;
    private AdapterGiveAwayGifts adapterGiveAwayGifts;
    private boolean isAll = false;
    private String appointment_id;//约会ID
    private String to_uid;//发起约会人ID

    @Override
    protected void bindPresenter() {
        giveAwayGiftsPresenter = new GiveAwayGiftsPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        appointment_id = getIntent().getStringExtra("appointment_id");
        to_uid = getIntent().getStringExtra("to_uid");
        showLod("加载中...");
        giveAwayGiftsPresenter.requestGetMyGiftList();
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_give_away_gifts;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setText("我的礼物");
    }


    @OnClick({R.id.iv_toobar_back, R.id.tv_give_away, R.id.ll_all_box, R.id.ll_store})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
            case R.id.tv_give_away:
                //赠送
                int i = 0;
                JSONArray gift_list = new JSONArray();
                for (MyGiftsResultBean.DataBean dataBean : myGiftsResultBean.data) {
                    if (dataBean.isCheck && dataBean.current_gift_num > 0) {
                        i++;
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("gift_id", dataBean.gift_id);
                        jsonObject.put("gift_num", dataBean.current_gift_num);
                        gift_list.add(jsonObject);
                    }
                }
                if (i > 0) {
                    giveAwayGiftsPresenter.sendGift(to_uid, appointment_id, gift_list);
                } else {
                    showT("请您选择礼物！");
                }
                break;
            case R.id.ll_all_box:
                if (isAll) {
                    //全选了  进行全不选
                    for (MyGiftsResultBean.DataBean dataBean : myGiftsResultBean.data) {
                        dataBean.isCheck = false;
                    }
                    adapterGiveAwayGifts.notifyDataSetChanged();
                    ivAll.setImageResource(R.drawable.unchecked);
                    tvAll.setText("全选");
                    isAll = false;
                    tvGiveAway.setText("赠送(0)");
                    tvTotalPrice.setText("￥0.0");
                } else {
                    //全不选  进行全选
                    double totalPirce = 0;
                    for (MyGiftsResultBean.DataBean dataBean : myGiftsResultBean.data) {
                        dataBean.isCheck = true;
                        totalPirce += dataBean.price * dataBean.current_gift_num;
                    }
                    adapterGiveAwayGifts.notifyDataSetChanged();
                    ivAll.setImageResource(R.drawable.checked);
                    tvAll.setText("全不选");
                    isAll = true;
                    tvGiveAway.setText("赠送(" + myGiftsResultBean.data.size() + ")");
                    tvTotalPrice.setText("￥" + totalPirce);
                }
                break;
            case R.id.ll_store:
                //跳转到我的商城界面
                Intent intent = new Intent(GiveAwayGiftsActivity.this, MyGiftsStoresActivity.class);
                intent.putExtra("flag", "flag");
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        giveAwayGiftsPresenter.requestGetMyGiftList();
    }

    @Override
    public void setAdapter(MyGiftsResultBean myGiftsResultBean) {
        this.myGiftsResultBean = myGiftsResultBean;
        adapterGiveAwayGifts = new AdapterGiveAwayGifts(myGiftsResultBean);
        adapterGiveAwayGifts.setOnItemClickListener(this);
        rvMyGifts.setLayoutManager(new LinearLayoutManager(this));
        rvMyGifts.setAdapter(adapterGiveAwayGifts);
    }

    @Override
    public void startInSomeDetailsActivity() {
        Intent intent = new Intent(this, InSomeDetailsActivity.class);
        intent.putExtra("appointment_id", appointment_id);
        startActivity(intent);
        finish();
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    @Override
    public void onAddNumClickListener() {
        onCheckBoxClickListener();
    }

    @Override
    public void onDeleteNumClickListener() {
        onCheckBoxClickListener();
    }

    @Override
    public void onCheckBoxClickListener() {
        int i = 0;//操作几个礼物对象
        double totalPirce = 0;
        for (MyGiftsResultBean.DataBean dataBean : myGiftsResultBean.data) {
            if (dataBean.isCheck) {
                i++;
                totalPirce += dataBean.price * dataBean.current_gift_num;
            }
        }
        if (i == myGiftsResultBean.data.size()) {
            //全选了
            isAll = true;
            ivAll.setImageResource(R.drawable.checked);
            tvAll.setText("全不选");
        } else {
            //没全选
            isAll = false;
            ivAll.setImageResource(R.drawable.unchecked);
            tvAll.setText("全选");
        }
        tvGiveAway.setText("赠送(" + i + ")");
        tvTotalPrice.setText("￥" + totalPirce);
    }


}
